<?php defined('SYSPATH') or die('No direct script access.');
/**
 * controller class for automatic templating.
 *
 */
class Controller_App_Template extends Kohana_Controller_Template {

    protected $_user, $_uid;
    protected $_post, $_query;
    protected $_shop;
    
	/**
	 * Loads the UserInfo.
	 */
	public function before()
	{
		parent::before();

        $this->_post  = $this->request->post();
        $this->_query = $this->request->query();

        $request = Request::initial();
        $reqinfo = array(
            'directory'  => $request->directory(),
            'controller' => $request->controller(),
            'action'     => $request->action(),
        );

        $controller = $request->controller();
        $action     = $request->action();
        if ($controller == 'weixin' && $action == 'notify')
        {
            $this->_user = new User_V2_Guest(); 
            $this->_uid  = 0;
        }
        else
        {
            $this->_user = user::currentUser();
            $this->_uid  = $this->_user->getUserUid();
        }

        $this->_shop = App_Shop::current();

        $this->template->set('versionCode', '1234567890');
        $this->template->set('request', $reqinfo);
        $this->template->set('global_user', array(
                'userUid'  => $this->_user->getUserUid(),
                'userName' => $this->_user->getUserName(),
                'userIcon' => $this->_user->getUserIcon(),
                'userGender' => $this->_user->getUserGender(),
            ));
        $this->template->set('global_shop', $this->_shop);
        $this->template->set('title', '会员中心');

        $this->footer_display_config();

		return TRUE;
	}
	
	protected function pagination ($count, $limit = 10, $page_type = 'route', $page_key = 'param1', $views = 'pagination/floating')
    {
        $page_config = array(
            'current_page'      => array('source' => $page_type, 'key' => $page_key),
            'total_items'       => (int) $count,
            'items_per_page'    => (int) $limit,
            'view'              => $views,
        );

		return Pagination::factory($page_config)->render();
    }
    
    protected function set_no_cache ()
    {
        header("Expires: Mon, 26 Jul 1970 05:00:00 GMT");
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        header("Cache-Control: no-cache, must-revalidate");
        header("Pragma: no-cache");
    }

    private function footer_display_config ()
    {
        $uri = Arr::get($_SERVER, 'REQUEST_URI');
        if (preg_match('#^/m(/.*)$#', $uri, $m))
        {
            $uri = $m[1];
        }

        if (strpos($uri,'share'))
            $actMenu = 'share';
        else if (preg_match('#^/(?:user|address|order)#', $uri))
            $actMenu = 'user';
        else
            $actMenu = 'homepage';

        $showMenu = TRUE;
        if (preg_match('#^/(?:goods/detail|chat)#', $uri))
        {
            $showMenu = FALSE;
        }

        $showChatIcon = TRUE;
        if (preg_match('#^/(?:chat|share)#', $uri))
        {
            $showChatIcon = FALSE;
        }
            
        
        $config = array(
            'showMenu' => $showMenu,
            'showChatIcon' => $showChatIcon,
            'activeMenu' => $actMenu,
        );

        $this->template->set('footer_config', $config);
    }

    // 初始化微信分享参数
    protected function initWechatShare ($title, $imgUrl, $desc = NULL, $link = NULL)
    {
        if (empty($link))
        {
            $link  = url::site($_SERVER['REQUEST_URI'], 'http', FALSE);
        }

        $wechatShare = array(
            'title'  => $title, 
            'desc'   => (string) $desc,
            'imgUrl' => $imgUrl,
            'link'   => $link,
        );

        $this->template->set('weixin', $wechatShare);

        $obj = new WxJssdk();
        $SignPackage = $obj->getSignPackage();
        $this->template->set('SignPackage', $SignPackage);
    }

    protected function json_encode($result)
    {
        echo json_encode($result);
        exit;
    }

    protected function userShouldLogin()
    {
        if ($this->_uid == 0)
        {
            $this->request->redirect('/login');
            return;
        }
    }

    protected function initApp ($title, $bottomBar = 'homepage') 
    {
        $displayBottomBar = empty($bottomBar) ? 'none' : 'block';
        $barName          = empty($bottomBar) ? 'homepage' : $bottomBar;

        $title_json  = App_Util::buildClientTitleJson('back', 'block', $title, 'message', 'none', '');
        $bottom_json = App_Util::buildClientBottomJson($displayBottomBar, $barName);
        
        $this->template->set('title_json',   $title_json);
        $this->template->set('bottom_json',  $bottom_json);
    }
 
} // End Controller_Template
